package com.axin.web.config;

import com.axin.web.interceptor.ApiAuthorizationInterceptor;
import com.axin.web.resolver.CurrentUserHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author lixin
 * @date 2021-07-01 17:44
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	/**
	 * 登录校验拦截器
	 * @return
	 */
	@Bean
	public ApiAuthorizationInterceptor apiAuthorizationInterceptor() {
		return new ApiAuthorizationInterceptor();
	}

	/**
	 * CurrentUser 注解参数解析器
	 * @return
	 */
	@Bean
	public CurrentUserHandlerMethodArgumentResolver currentUserHandlerMethodArgumentResolver() {
		return new CurrentUserHandlerMethodArgumentResolver();
	}


	/**
	 * 拦截器注册
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry
			.addInterceptor(apiAuthorizationInterceptor())
			.addPathPatterns("/**");
	}

	/**
	 * 参数解析器
	 *
	 * @param argumentResolvers
	 */
	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		argumentResolvers.add(currentUserHandlerMethodArgumentResolver());
	}





}
